<?php

namespace app\task\model;

use app\api\controller\Controller as ApiController;
use app\common\model\Order as OrderModel;
use app\api\model\OrderAddress;
use app\store\model\User;
use think\Db;

/**
 * 订单模型
 * Class Order
 * @package app\common\model
 */
class Order extends OrderModel
{
    /**
     * 待支付订单详情
     * @param $order_no
     * @return null|static
     * @throws \think\exception\DbException
     */
    public function payDetail($order_no)
    {
        return self::get(['order_no' => $order_no, 'pay_status' => 10], ['goods']);
    }

    /**
     * 更新付款状态
     * @param $transaction_id
     * @return false|int
     * @throws \Exception
     */
    public function updatePayStatus($transaction_id)
    {
        Db::startTrans();
        // 更新商品库存、销量
        $GoodsModel = new Goods;
        $GoodsModel->updateStockSales($this['goods']);
        // 更新订单状态
        $this->save([
            'pay_status' => 20,
            'pay_time' => time(),
            'transaction_id' => $transaction_id,
        ]);
        Db::commit();
        return true;
    }

    /**
     * 发送模板消息
     * 1、通知付款人； 2、通知服务员接单
     * @param $data array 微信服务器返回数据
     * @param $order string 订单信息
     * @return false|int
     * @throws \Exception
     */
    public function sendTemplateMessage($data, $order)
    {

        #--支付成功，向下单用户发起消息推送：
        $param = [
            'touser' => $data['openid'],
            'template_id' => 'E_BCAyyl6KizZoj_K7LX8D-nnffLdL4D1NSK-efU2CA',
            'url' => "https://" . $_SERVER['SERVER_NAME'],
            'form_id' => $order['prepay_id'],
            'topcolor' => "#FF0000",
            'data' => array(
                #'first' => array('value' => "下单成功", 'color' => "#fc0101"),
                'keyword1' => array('value' => $order['total_price'], 'color' => "#173177"), //订单金额
                'keyword2' => array('value' => $order['pay_price'], 'color' => "#173177"), //支付金额
                'keyword3' => array('value' => $order["goods"][0]['goods_name'], 'color' => "#173177"), //商品名称
                'keyword4' => array('value' => $data['out_trade_no'], 'color' => "#173177"), //订单编号
                'keyword5' => array('value' => $order['remark'], 'color' => "#173177"), //备注
                #'remark' => array('value' => "您的订单已支付成功，配送员即将为您配送", 'color' => "#173177"),
            )
        ];
        send_template_message($param);

        #--如果有指定服务员，向服务员推送员工接单通知：
        $user = new \app\api\model\User();
        if ($order['b_id']) {

            #--获取formId：
            $formId = $user->getFormId($order['b_id']);

            $userInfo = $user->get(['user_id' => $order['b_id']]);
            $address = OrderAddress::get(['order_id' => $order['order_id']]);
            $param = [
                'touser' => $userInfo['open_id'],
                'template_id' => 'aPyXk5uJbbU9gd-6kKQvINaKxWCt3OIG-JzDEkASKMU',
                'url' => "https://" . $_SERVER['SERVER_NAME'],
                'form_id' => $formId[0],
                'topcolor' => "#FF0000",
                'data' => array(
                    #'first' => array('value' => "你有新的订单", 'color' => "#fc0101"),
                    'keyword1' => array('value' => $data['out_trade_no'], 'color' => "#173177"),    #--订单号
                    'keyword2' => array('value' => date('Y-m-d H:i:s', time()), 'color' => "#173177"),    #--配送时间
                    'keyword3' => array('value' => $address['name'], 'color' => "#173177"),    #--用户
                    'keyword4' => array('value' => $address['phone'], 'color' => "#173177"),    #--联系电话
                    'keyword5' => array('value' => $address['region']['city'] . "  " . $address['region']['region'] . "  " . $address['detail'], 'color' => "#173177"),    #--收货地址
                    #'remark' => array('value' => "新订单已产生，请及时接单", 'color' => "#173177"),
                )
            ];
            send_template_message($param);

            #--修改服务员工作状态：
            (new User)->save(['isworking' => 'Y'], ['open_id' => $userInfo['open_id']]);
        }

        #--给指定后台管理员发送消息，为新订单指定配送员：
        $formId = $user->getFormId(27);
        $userInfo = $user->get(['user_id' => 27]);
        $address = OrderAddress::get(['order_id' => $order['order_id']]);
        $param = [
            'touser' => $userInfo['open_id'],
            'template_id' => 'aPyXk5uJbbU9gd-6kKQvINaKxWCt3OIG-JzDEkASKMU',
            'url' => "https://" . $_SERVER['SERVER_NAME'],
            'form_id' => $formId[0],
            'topcolor' => "#FF0000",
            'data' => array(
                #'first' => array('value' => "你有新的订单", 'color' => "#fc0101"),
                'keyword1' => array('value' => $data['out_trade_no'], 'color' => "#173177"),    #--订单号
                'keyword2' => array('value' => date('Y-m-d H:i:s', time()), 'color' => "#173177"),    #--配送时间
                'keyword3' => array('value' => $address['name'], 'color' => "#173177"),    #--用户
                'keyword4' => array('value' => $address['phone'], 'color' => "#173177"),    #--联系电话
                'keyword5' => array('value' => $address['detail'] . ",请及时到后台指定配送员。当前可接收消息总量：".$formId[1], 'color' => "#173177"),    #--收货地址
                #'remark' => array('value' => "新订单已产生，请及时接单", 'color' => "#173177"),
            )
        ];
        send_template_message($param);


    }

}
